<?php 
	//负责处理虚拟币相关的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class coin extends  base{
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			error_reporting(E_ALL ^ E_DEPRECATED);
			set_time_limit(0);
			parent::__construct();
		}
		//开始自动程序
		public function run(){
			$this->runCoinPay();
		}
		//自动检测充币程序
		private function runCoinPay(){
			//获取所有用户以太坊地址
			$coin = $this->getETHAddress();
			foreach ($coin as $coin_k=>$coin_v){
			    if($coin_v['url3']){
			    //获取钱包地址里有没有余额
			    $usercoin = "http://39.104.56.81:8080/ethapi/getBalance.do?address=".$coin_v['url3'];
			    $result=$this->curl_get($usercoin);
			    //dump($result);
			    if($result['data']>=0.01&&$result['code']==200){
			        //echo 1;
			        //交易
			        $usermiyao = $this->getUserMiyao($coin_v['uid'])['url'];
					$money = $result['data'];
					$shiji_money = $money-0.0009;
			        //dump($shiji_money);
			        //收以太坊的钱包地址
			        //$inetcaddress = '0xE8c4C0EF6717766B5a5E2fe9c462DC86Ea75c2BA';//以前的
					$inetcaddress = '0x3B9B2286fa32317D512abD563973254AC74922ad';					
			        $cointrade = "http://39.104.56.81:8080/ethapi/sendTransaction.do?out=".$coin_v['url3']."&in=".$inetcaddress."&value=".$shiji_money."&password=".$usermiyao."&gasPrice=10000000000";
			        $tradestatus=$this->curl_get($cointrade);
			       // dump($tradestatus);
			    }
			    
				//对应地址查询属于哪个人
				$user = $this->getUserByCoinAddress($coin_v['url3']);
				if(!$user){
					continue;
				}
				if($result['data']>=0.01&&$result['code']==200){
				//换算相当于充值了多少钱
				if($tradestatus['code']==200){
				    $money_shiji = round($money,2);
				//加充值表
				$this->addCoinPay($user['uid'],$coin_v['url3'],'',$money_shiji,'',3,'',1);
				//加钱 加财务日志
				$this->addUserCoin($user['uid'], $money_shiji,3);
				}
				}
			}
			}
		}
		//使用curl的GET请求数据  返回数组
		protected function curl_get($http){
		    //初始化
		    $ch = curl_init();
		    //设置选项，包括URL
		    curl_setopt($ch, CURLOPT_URL, $http);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		    curl_setopt($ch, CURLOPT_HEADER, 0);
		    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。https不好使加这个
		    //执行并获取HTML文档内容
		    $output = curl_exec($ch);
		    //释放curl句柄
		    curl_close($ch);
		    //打印获得的数据
		    $result = json_decode($output,true);
		    return $result;
		}
		//加用户币
		private function addUserCoin($uid,$money,$money_type){
			//处理金额变动
			$sql ="
			UPDATE yang_member_money
			SET money".$money_type." = money".$money_type."+".$money."
			WHERE member_id=".$uid;
			//dump($sql);
			$this->query($sql);			
		}
		//查询钱包最近的充值记录
		private function linkCoinQianbao($name,$pwd){
			$easycoin=new Easycoin($name,$pwd);
			return $easycoin;
		}
		//获取以太坊地址
		private function getETHAddress(){
			$sql = "SELECT uid,url3 FROM yang_wallet";
			return $this->select($sql);
		}
		//获取秘钥
		private function getUserMiyao($uid){
		    $sql = "SELECT uid,url FROM yang_miyao WHERE uid =".$uid;
		    return $this->find($sql);
		}
		//根据txid获取充币记录
		private function getCoinPayLogByTxid($txid){
			$sql = "SELECT id FROM yang_coin_pay WHERE status = 1 AND txid = '".$txid."'";
			return $this->find($sql);
		}
		//根据地址查询会员账号
		private function getUserByCoinAddress($address){
			$sql = "SELECT * FROM yang_wallet WHERE url3 ='".$address."'";
			return $this->find($sql);
		}
		//增加充值订单表
		private function addCoinpay($uid,$url,$name,$num,$bili,$coin_type,$txid,$status){
			$sql = "INSERT INTO yang_coin_pay(uid,url,name,num,bili,coin_type,add_time,txid,status) 
					VALUES('".$uid."','".$url."','".$name."','".$num."','".$bili."','".$coin_type."','".time()."','".$txid."','1')";
			$this->query($sql);
		}
		//充值成功 修改状态
		private function updateCoinPayByTxid($txid){
			$sql = "UPDATE yang_coin_pay set status = 1 , check_time =".time()." where txid='".$txid."'";
			$this->query($sql);
		}	
	}
	$coin=new coin();
	$coin->run();
?>